const fs = require('fs');
const path = require('path');
const db = require('../config/db');

async function initDatabase() {
  try {
    // 读取 SQL 文件
    const sql = fs.readFileSync(path.join(__dirname, 'schema.sql'), 'utf8');

    // 按语句分割
    const statements = sql.split(';').filter(stmt => stmt.trim());

    // 执行每个语句
    for (let statement of statements) {
      if (statement.trim()) {
        await db.query(statement);
        console.log('执行 SQL:', statement.slice(0, 50) + '...');
      }
    }

    console.log('数据库初始化成功！');
    process.exit(0);
  } catch (error) {
    console.error('数据库初始化失败:', error);
    process.exit(1);
  }
}

initDatabase(); 